package com.mazaiting.generator.controller;

import com.mazaiting.common.core.domain.result.Option;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.generator.domain.form.ProjectForm;
import com.mazaiting.generator.domain.info.ProjectInfo;
import com.mazaiting.generator.domain.query.ProjectQuery;
import com.mazaiting.generator.domain.vo.ProjectVO;
import com.mazaiting.generator.service.IProjectService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 项目名
 */
@RestController
@RequestMapping("generator/v1/project")
@AllArgsConstructor
public class ProjectController {

    private IProjectService projectService;

    /**
     * 工程列表分页数据
     * @param projectQuery 查询条件
     * @return 分页数据
     */
    @GetMapping("page")
    public BasePageVO<ProjectInfo> page(@Valid ProjectQuery projectQuery) {
        return projectService.page(projectQuery);
    }

    /**
     * 根据工程 ID 获取工程信息
     * @param id 工程 ID
     * @return 工程信息
     */
    @GetMapping("{id}")
    public ProjectVO get(@PathVariable("id") Long id) throws EmptyObjectException {
        return projectService.getInfoById(id);
    }

    /**
     * 新增工程信息
     * @param projectForm 工程信息
     * @return true: 新增成功; false: 新增失败
     */
    @PostMapping
    public boolean save(@Valid ProjectForm projectForm) {
        return projectService.save(projectForm);
    }

    /**
     * 修改工程信息
     * @param projectForm 工程信息
     * @return true: 修改成功; false: 修改失败
     */
    @PutMapping
    public boolean update(@Valid ProjectForm projectForm) throws EmptyObjectException {
        return projectService.update(projectForm);
    }

    /**
     * 根据 ID 删除数据
     * @param ids ID 列表
     * @return true: 删除成功; false: 删除失败
     */
    @DeleteMapping
    public boolean delete(@RequestParam("ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        return projectService.removeByIds(idList);
    }

    /**
     * 工程下拉列表
     *
     * @return 工程下拉列表
     */
    @GetMapping("/options")
    public List<Option<Long>> listOptions() throws BusinessException {
        return projectService.listOptions();
    }

}